/**
 * <pre>
 * @(#)ChartView.java 1.00 2009-11-6
 * 
 * Copyright (c) 1998, 2009 Regular Individual. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Contributor(s):
 *     Haixu Huang
 * </pre>
 **/
package cn.sh.huang.chart.view;

import java.awt.Dimension;
import java.awt.Graphics2D;

import cn.sh.huang.chart.data.DataTable;

/**
 * <pre>
 * ChartView ...
 * 
 * @version 1.0
 * </pre>
 */
abstract public class ChartView extends BufferView
{
    protected DataTable data = null;

    public void setData(DataTable data)
    {
        if (data == null || !validateData(data)) {
            throw new IllegalArgumentException("!ERROR! Invalid data format.");
        }
        this.data = data;
        dataCast();
    }

    public void doUpdate()
    {
        dataCast();
        super.doUpdate();
    }

    /*
     * (non-Javadoc)
     * @see cn.sh.huang.chart.view.BufferView#renderGraph(java.awt.Image)
     */
    protected void renderBuffer(Graphics2D g2d)
    {
        drawDataShape(g2d);
    }

    private void dataCast()
    {
        Dimension viewSize = getViewSize();
        if (viewSize.width > 0 && viewSize.height > 0) {
            setDataShape();
        }
    }

    abstract void setDataShape();

    abstract void drawDataShape(Graphics2D g2d);

    abstract protected boolean validateData(DataTable _data);
}
